import java.awt.EventQueue;

import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JList;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.Hashtable;

import javax.swing.JRadioButton;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;


public class Admin {

	public JFrame frmAdmin;
	private JTextField textField;
	private JButton btnSubmit;
    private JButton btnCancel;
    private JButton btnSearch;
    private DefaultListModel lm;
    private JList userList;
    private JRadioButton rdbtnWrite;
    private JRadioButton rdbtnAdmin;
    private JRadioButton rdbtnRead;
	
    Hashtable<String,String> theUsers;
    private JButton edits;
    
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Admin window = new Admin();
					window.frmAdmin.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public Admin() {
		initialize();
		Database db = new Database();
		ArrayList<User> users = db.getAllUsers();
		populateUsers(users);
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmAdmin = new JFrame();
		frmAdmin.setTitle("Admin");
		frmAdmin.setBounds(100, 100, 450, 300);
		frmAdmin.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frmAdmin.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		frmAdmin.getContentPane().setLayout(null);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 11, 142, 240);
		frmAdmin.getContentPane().add(scrollPane);
		
		
		//lm.addElement();
		//right here 
		//right here
		//right here
		//right here
		//right here
		
		lm = new DefaultListModel();
		
		userList = new JList(lm);
		userList.addListSelectionListener(new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent arg0) {
				String privs = theUsers.get(userList.getSelectedValue().toString());
				if(privs.contains("a")){
					rdbtnAdmin.setSelected(true);
					rdbtnWrite.setSelected(false);
					rdbtnRead.setSelected(false);
				}else if(privs.contains("w")){
					rdbtnWrite.setSelected(true);
					rdbtnAdmin.setSelected(false);
					rdbtnRead.setSelected(false);
				}else{
					rdbtnRead.setSelected(true);
					rdbtnAdmin.setSelected(false);
					rdbtnWrite.setSelected(false);
				}
			}
		});
		scrollPane.setViewportView(userList);
		userList.setSelectedIndex(0);
		
		textField = new JTextField();
		textField.setBounds(178, 31, 116, 20);
		frmAdmin.getContentPane().add(textField);
		textField.setColumns(10);
		
	    btnSubmit = new JButton("Submit");
	    btnSubmit.addActionListener(new ActionListener() {
	    	public void actionPerformed(ActionEvent e) {
	    		submit();
	    	}
	    });
		btnSubmit.setBounds(335, 193, 89, 23);
		frmAdmin.getContentPane().add(btnSubmit);
		
		btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				frmAdmin.setVisible(false);
			}
		});
		btnCancel.setBounds(240, 193, 89, 23);
		frmAdmin.getContentPane().add(btnCancel);
		
		btnSearch = new JButton("Search");
		btnSearch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				searching();
			}
		});
		btnSearch.setBounds(304, 30, 89, 23);
		frmAdmin.getContentPane().add(btnSearch);
		
		rdbtnWrite = new JRadioButton("Write");
		rdbtnWrite.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				rdbtnAdmin.setSelected(false);
				rdbtnRead.setSelected(false);
				if(!rdbtnWrite.isSelected()){
					rdbtnWrite.setSelected(true);
				}
			}
		});
		rdbtnWrite.setBounds(158, 113, 109, 23);
		frmAdmin.getContentPane().add(rdbtnWrite);
		
		rdbtnAdmin = new JRadioButton("Admin");
		rdbtnAdmin.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				rdbtnWrite.setSelected(false);
				rdbtnRead.setSelected(false);
				if(!rdbtnAdmin.isSelected()){
					rdbtnAdmin.setSelected(true);
				}
			}
		});
		rdbtnAdmin.setBounds(158, 87, 109, 23);
		frmAdmin.getContentPane().add(rdbtnAdmin);
		
		rdbtnRead = new JRadioButton("Read");
		rdbtnRead.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				rdbtnAdmin.setSelected(false);
				rdbtnWrite.setSelected(false);
				if(!rdbtnRead.isSelected()){
					rdbtnRead.setSelected(true);
				}
			}
		});
		rdbtnRead.setBounds(158, 137, 109, 23);
		frmAdmin.getContentPane().add(rdbtnRead);
		
		edits = new JButton("View Edits");
		edits.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				viewEdits ed = new viewEdits();
				ed.frmEdits.setVisible(true);
			}
		});
		edits.setBounds(304, 227, 120, 23);
		frmAdmin.getContentPane().add(edits);
		
		
	}
	
	public void searching(){
		
	    ArrayList<User> list = new ArrayList<User>();
		String str = textField.getText().toString();
		Database db = new Database();
		
		list = db.lookupUser(str);
		
		populateUsers(list);
		
	}
	
	public void submit(){
		Database db = new Database();
		String priv;
		String curUser = userList.getSelectedValue().toString();
		if(rdbtnAdmin.isSelected()){
			priv = "rwa";
		}else if(rdbtnWrite.isSelected()){
			priv = "rw";
		}else{
			priv = "r";
		}
		System.out.println(db.updateUser(curUser, priv));
		frmAdmin.setVisible(false);
	}
	
	public void populateUsers(ArrayList<User> u){
		lm.clear();
		theUsers = new Hashtable<String,String>();
		for(int i = 0; i < u.size(); i++){
			String name = u.get(i).username;
			String pr = u.get(i).privs;
			theUsers.put(name, pr);
			if(!MainScreen.theUser.equals(name)){
				lm.addElement(name);
			}
		}
	}
}
